<template>
  <div class="list-table">
    <div class="area-header">
      <span class="area-header-title">{{ $t('候选人') }}</span>
    </div>
    <el-form ref="queryForm" :model="queryParams" :inline="true">
      <el-form-item :label="$t('姓名')" prop="contactsName">
        <el-input
          v-model="queryParams.contactsName"
          :placeholder="$t('姓名')"
          clearable
          @keyup.enter.native="queryHandle"
        />
      </el-form-item>
      <el-form-item :label="$t('所属公司')" prop="company">
        <el-input
          v-model="queryParams.company"
          :placeholder="$t('所属公司')"
          clearable
          @keyup.enter.native="queryHandle"
        />
      </el-form-item>
      <el-form-item v-if="!getBusinessId" :label="$t('业务类型')" prop="businessType">
        <dictSelect
          v-model="queryParams.businessType"
          :clearable="true"
          dictionary="main_body_type_all"
        />
      </el-form-item>
      <el-form-item v-if="!getBusinessId" :label="$t('业务名称')" prop="businessId">
        <MainBodySelect
          v-model="queryParams.businessId"
          :parameters="queryParams.businessType"
          :placeholder="$t('请先选择业务类型')"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="queryHandle">{{ $t("搜索") }}</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">{{ $t("重置") }}</el-button>
      </el-form-item>
    </el-form>
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          v-hasPermi="['contact:person:edit']"
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="addHandle"
        >{{ $t("新增") }}</el-button>
      </el-col>
      <el-col v-if="false" :span="1.5">
        <el-button
          v-hasPermi="['contact:person:export']"
          type="warning"
          icon="el-icon-download"
          size="mini"
          @click="exportHandle"
        >{{ $t("导出") }}</el-button>
      </el-col>
    </el-row>

    <el-table v-loading="loading" tooltip-effect="darkTable" :data="dealContactsList" border>
      <el-table-column :label="$t('序号')" type="index" align="center" show-overflow-tooltip width="50">
        <template slot-scope="scope">
          {{ (queryParams.pageNum - 1) * queryParams.pageSize + scope.$index + 1 }}
        </template>
      </el-table-column>
      <!--<el-table-column v-if="!this.$route.query.id" :label="业务类型" align="center" prop="businessType" />-->
      <!--<el-table-column v-if="!this.$route.query.id" :label="业务名称" align="center" prop="businessId" />-->
      <el-table-column :label="$t('姓名')" align="center" prop="contactsName" />
      <el-table-column :label="$t('电话')" align="center" prop="phone" />
      <el-table-column :label="$t('所属公司')" align="center" prop="company" />
      <el-table-column :label="$t('职位')" align="center" prop="position" />
      <el-table-column :label="$t('邮箱')" align="center" prop="email" />
      <el-table-column :label="$t('微信')" align="center" prop="weChat" />
      <el-table-column :label="$t('操作')" align="center" class-name="small-padding fixed-width" width="200">
        <template slot-scope="scope">
          <el-button
            v-hasPermi="['contact:person:edit']"
            size="small"
            type="text"
            icon="el-icon-edit"
            @click="updateHandle(scope.row)"
          >{{ $t('修改') }}</el-button>
          <el-button
            v-hasPermi="['contact:person:remove']"
            size="small"
            type="text"
            icon="el-icon-delete"
            @click="deleteHandle(scope.row)"
          >{{ $t('删除') }}</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改项目联系人对话框 -->
    <el-dialog :title="$t(title)" :visible.sync="formOpen" :close-on-click-modal="false" :lock-scroll="false" custom-class="dialogForm" append-to-body>
      <contactPersonForm ref="contactPersonForm" :bus-type="busType || dealData.busType" />
    </el-dialog>
  </div>
</template>

<script>
import {
  listDealContacts,
  delDealContacts
} from '@/api/sass/yueyin/contact/YueYinContactsPerson'
import contactPersonForm from './contactPersonForm.vue'
import MainBodySelect from '@/components/MainBodySelect/index'
export default {
  components: {
    contactPersonForm,
    MainBodySelect
  },
  props: {
    busType: {
      type: String,
      default: undefined
    }
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 总条数
      total: 0,
      // 项目联系人表格数据
      dealContactsList: [],
      // 弹出层标题
      title: '候选人',
      // 是否显示弹出层
      formOpen: false,
      // 查询参数
      queryParams: {
        businessType: undefined,
        businessId: undefined,
        contactsName: undefined,
        company: undefined,
        pageNum: 1,
        pageSize: 10
      },
      // 业务id
      getBusinessId: this.$route.query.id,
      // 表单参数
      form: {},
      // 项目data
      dealData: '',
      // 表单校验
      rules: {
        contactsName: [
          { required: true, message: this.$t('必填信息'), trigger: 'blur' }
        ],
        phone: [{ required: true, message: this.$t('必填信息'), trigger: 'blur' }],
        company: [
          { required: true, message: this.$t('必填信息'), trigger: 'blur' }
        ],
        position: [
          { required: true, message: this.$t('必填信息'), trigger: 'blur' }
        ],
        email: [{ required: true, message: this.$t('必填信息'), trigger: 'blur' }]
      }
    }
  },
  mounted() {
    this.getBusinessIdMethod()
    this.$bus.$emit('async_done')
  },
  created() {
    this.getList()
  },
  methods: {
    init(data) {
      this.dealData = data
    },
    /** 查询项目联系人列表 */
    getList() {
      this.loading = true
      if (this.getBusinessId) {
        this.getBusinessIdMethod()
      }
      listDealContacts(this.queryParams).then(response => {
        this.dealContactsList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    getBusinessIdMethod() {
      if (this.getBusinessId === undefined || this.getBusinessId === '' || this.getBusinessId === null) {
        if (this.$route.path.includes('lpBase')) {
          this.getBusinessId = this.$route.query.lpId
        }
      }
      this.queryParams.businessId = this.getBusinessId
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        dealId: undefined,
        contactsName: undefined,
        phone: undefined,
        company: undefined,
        position: undefined,
        email: undefined,
        weChat: undefined,
        remark: undefined,
        createBy: undefined,
        createTime: undefined,
        updateBy: undefined,
        updateTime: undefined,
        delFlag: undefined
      }
      this.resetForm('form')
    },
    /** 搜索按钮操作 */
    queryHandle() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm('queryForm')
      this.queryHandle()
    },
    /** 新增按钮操作 */
    addHandle() {
      this.formOpen = true
      this.$nextTick(() => {
        this.$refs.contactPersonForm.init(null)
      })
    },
    /** 修改按钮操作 */
    updateHandle(row) {
      this.formOpen = true
      this.$nextTick(() => {
        // 调用form页面的 init 方法初始化数据
        this.$refs.contactPersonForm.init(row)
      })
    },
    /** 删除按钮操作 */
    deleteHandle(row) {
      this.$confirm(this.$t('是否确认删除?'), this.$t('警告'), {
        confirmButtonText: this.$t('确定'),
        cancelButtonText: this.$t('取消'),
        type: 'warning'
      })
        .then(function() {
          return delDealContacts(row.id)
        })
        .then(() => {
          this.getList()
          this.msgSuccess(this.$t('删除成功'))
        })
        .catch(function() {})
    },
    /** 导出按钮操作 */
    exportHandle() {
      this.downLoadExcel('/yueyin/contact/contactPerson/export', this.queryParams)
    }
  }
}
</script>
